#pragma once
#include<assert.h>


namespace lcy
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		const iterator begin() const
		{
			return _str;
		}
		const iterator end() const
		{
			return _str + _size;
		}
		/*string()
			:_str(new char[1]{ '\0' })
			,_size(0)
			,_capacity(0)
		{}*/
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		char& operator[](size_t pos)
		{
			assert(pos >= 0 && pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos >= 0 && pos < _size);
			return _str[pos];
		}

		size_t capacity() const
		{
			return _capacity;
		}

		size_t size() const
		{
			return _size;
		}

		const char* c_str() const
		{
			return _str;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}

		void push_back(const char ch)
		{
			if (_size == _capacity)
			{
				reserve( _capacity == 0? 4 : _capacity * 2);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			size_t end = _size + 1;
			while (pos < end)
			{
				_str[end] = _str[end - 1];
				--end;
			}
			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)
		{}

		void erase(size_t pos, size_t len = npos)
		{}

		bool operator<(const string& s) const
		{
			return strcmp(_str, s._str) < 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

		bool operator<=(const string& s) const
		{
			return *this < s || *this == s;
		}

		bool operator>(const string& s) const
		{
			return !(*this <= s);
		}

		bool operator>=(const string& s) const
		{
			return!(*this < s);
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0; 
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;

		const static size_t npos;
	};

	const size_t string::npos = -1;

	ostream& operator<<(ostream& out, string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];	
		}
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch;	
		//in >> ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			//in >> ch; 
			ch = in.get();
		}
		return in;
	}

	void test_mystring1()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		
		for (int i = 0; i < s1.size(); i++)
		{
			cout << s1[i];
		}
		cout << endl;

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		for (auto ch : s1)
		{
			cout << ch << " ";
		}
		cout << endl;
	}

	void test_mystring2()
	{
		string s1 = "hello world";
		cout << s1.c_str() << endl;
		s1.push_back(' ');
		s1.append("chaoyuejiayou chaoyuejiayou!!!");
		cout << s1.c_str() << endl;
		s1 += '*';
		s1 += "&&&&&";
		cout << s1.c_str() << endl;
	}

	void test_mystring3()
	{
		string s1 = "hello world";
		cout << s1.c_str() << endl;

		s1.insert(5, '*');
		cout << s1.c_str() << endl;
		s1.insert(0, '*');
		cout << s1.c_str() << endl;
		s1.insert(s1.size(), '*');
		cout << s1.c_str() << endl;

		cin >> s1;
		cout << s1;
	}
}